﻿//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System;
using System.Diagnostics;
using System.Reflection;
using Microsoft.SharePoint.WebControls;

namespace AWS.SLAM.Administration
{
    public partial class SLAMControlPanel : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Test if the 3.5 Framework is installed
                if (!InstallationValidator.VerifyNetFx35IsInstalled())
                {
                    installationMessage.Visible = false;
                    welcomeMessage.Visible = false;
                    errorMessage.Visible = true;
                    ltErrorMessage.Text = "SLAM requires the .NET Framework 3.5 and it appears that this SharePoint site's server does not have it installed.  Please install it then check this page again.";
                }
                else
                {
                    //Test if SLAM can clear instances, if not the slam.config is likely missing
                    //and if that is not the case, show the error
                    try
                    {
                        ComponentService.ClearInstances();
                    }
                    catch (Exception ex)
                    {
                        if (ex.ToString().Contains("System.IO.FileNotFoundException"))
                        {
                            installationMessage.Visible = true;
                            welcomeMessage.Visible = false;
                            errorMessage.Visible = false;
                            
                            if (ex is System.IO.FileNotFoundException)
                            {
                                ltConfigPath.Text = "<br /><br/ >SLAM attempted to locate its config at this location and failed to do so: " + ((System.IO.FileNotFoundException)ex).FileName;
                            }
                            else if (ex.InnerException != null && ex.InnerException is System.IO.FileNotFoundException)
                            {
                                ltConfigPath.Text = "<br /><br/ >SLAM attempted to locate its config at this location and failed to do so: " + ((System.IO.FileNotFoundException)ex.InnerException).FileName;
                            }
                            else if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException is System.IO.FileNotFoundException)
                            {
                                ltConfigPath.Text = "<br /><br/ >SLAM attempted to locate its config at this location and failed to do so: " + ((System.IO.FileNotFoundException)ex.InnerException.InnerException).FileName;
                            }
                        }
                        else
                        {
                            installationMessage.Visible = false;
                            welcomeMessage.Visible = false;
                            errorMessage.Visible = true;
                            ltErrorMessage.Text = ex.ToString();
                        }
                    }
                }

                ltVersion.Text = "Version: " + FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
            }
        }
    }
}
